package cn.edu.sctu.sctu_lysd.presenter;

import android.app.Activity;
import android.widget.EditText;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import cn.edu.sctu.sctu_lysd.bean.JsonBaseBeen;
import cn.edu.sctu.sctu_lysd.bean.ParcelMessageBean;
import cn.edu.sctu.sctu_lysd.bean.OrderData;
import cn.edu.sctu.sctu_lysd.biz.SubmitReleaseBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.CompressImgUtil;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-09.
 */

public class SubmitReleasePresenter implements SubmitReleaseBiz.Presenter {

    private Activity mActivity;
    private SubmitReleaseBiz.SubmitReleaseView mView;

    private SharePreferenceUtils mSharePreference;

    private NetService service;

    private Call<JsonBaseBeen<OrderData>> submitRelease;

    private CompressImgUtil compressImgUtil;


    public SubmitReleasePresenter(Activity activity, SubmitReleaseBiz.SubmitReleaseView mView) {
        this.mActivity = activity;
        this.mView = mView;

    }

    @Override
    public void start() {

        mSharePreference = SharePreferenceUtils.getInstance(mActivity);
        RetrofitHelper retrofitHelper = RetrofitHelper.getInstance();
        service = (NetService) retrofitHelper.getService(NetService.class);
        compressImgUtil = CompressImgUtil.getInstance();

    }

    @Override
    public String getInputSting(EditText view) {

        if (view != null) {
            return view.getText().toString();
        }

        return "";
    }

    @Override
    public void cancel() {
        if (submitRelease != null)
            submitRelease.cancel();
    }


    @Override
    public void selectPhoto() {

    }

    @Override
    public void uploadMessage(ParcelMessageBean parcelMessageBean) throws IOException {

        UUID uuid = UUID.randomUUID();
        String fileName = uuid + ".jpeg";

        /**
         * 压缩处理
         */
        compressImgUtil.compressImageByScale(parcelMessageBean.getFilePath(), CacheKeyConfig.CACHE_HOME, fileName);

        RequestBody requestBody =
                RequestBody.create(MediaType.parse("multipart/form-data"), new File(CacheKeyConfig.CACHE_HOME + "/" + fileName));

        submitRelease = service.parcels(
                mSharePreference.getString(CacheKeyConfig.CACHE_ACCESS_TOKEN),
                parcelMessageBean.getOriginPre(),
                parcelMessageBean.getOriginDetail(),
                parcelMessageBean.getDestinationPre(),
                parcelMessageBean.getDestinationDetail(),
                parcelMessageBean.getStartTime(),
                parcelMessageBean.getEndTime(),
                parcelMessageBean.getName(),
                parcelMessageBean.getDescription(),
                Double.parseDouble(parcelMessageBean.getWeight()),
                parcelMessageBean.getReceiverName(),
                parcelMessageBean.getReceiverPhone(),
                Double.parseDouble(parcelMessageBean.getTotalAmount()),
                requestBody);

        submitRelease.enqueue(new Callback<JsonBaseBeen<OrderData>>() {
            @Override
            public void onResponse(Call<JsonBaseBeen<OrderData>> call, Response<JsonBaseBeen<OrderData>> response) {

                L.e("submitReleaseresponsewangfuxin", "response" + response.toString());

                compressImgUtil.recursionDeleteFile(new File(CacheKeyConfig.CACHE_HOME));

                if (response.code() == 200 && response.body().getMeta().getCode() == 200) {

                    mView.uploadSuccess(response.body().getData().getId());
                } else {
                    mView.uploadFailed();

                }


            }

            @Override
            public void onFailure(Call<JsonBaseBeen<OrderData>> call, Throwable t) {

                compressImgUtil.recursionDeleteFile(new File(CacheKeyConfig.CACHE_HOME));

                mView.uploadFailed();

                L.e("responsewangfuxin", "错了");

                t.printStackTrace();
            }
        });


    }

    @Override
    public String getBundleData() {
        return "";
    }

}
